POV-Ray : Newsgroups : povray.general : I am having a lot of problems writting a recursive function : Re: I am having a lot of problems writting a recursive function Server Time
2 Aug 2024 20:15:32 EDT (-0400)
  Re: I am having a lot of problems writting a recursive function  
From: Slime
Date: 24 Aug 2004 22:28:00
Message: <412bf930$1@news.povray.org>
> I wound up having to have a macro call the second macro which called the
> third, etc until 16 or so.  So I wrote 16 macros, when I bet I could have
> done it with a simple recursive macro.  :(


Eek!

It looks like all of your macros are pretty much identical. All you need to
do is keep track of how many calls "deep" you are. You can do that with an
extra parameter, like this:

(I renamed "treeX" to "buildtree" so that it doesn't conflict with your
"tree" macro which actually makes an individual branch.)

#macro buildtree(aty,rotz, scl,  depth)
 union {
 tree(0,0,1)
 #if (depth = 15)
  leave(8,atan2d(4,3),.75)
  leave(7,-atan2d(3,4),.75)
 #else
  buildtree(8,atan2d(4,3),.75, depth+1) // depth increases with each call
  buildtree(7,-atan2d(3,4),.75, depth+1)
 #end
 scale <scl,scl,scl>
 rotate <0,72,rotz>
 translate <0,aty,0>
}

buildtree(0,0,1, 0 /* initial depth of zero */)

By the way, "leave" should probably be "leaf"; the v is only used for the
plural ("leaves").

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.